﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Linq;
using NHibernate.Criterion;
using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.NH;

using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    /// <summary>
    ///     Provides GetAll() which returns all languages in the application
    ///     and extends Find
    /// </summary>
    public class LangDao : BaseDao<Lang, int, LangFilter>, ILangDao
    {
        #region Find
        /// <summary>
        ///     Evaluates all filters and order-by clauses
        ///     and returns sorted list of matching Lang entities
        /// </summary>
        /// <param name = "filter">ISearchObject&lt;Lang&gt; containing LangFilter not-null instance</param>
        /// <returns>List of Lang entities matching the filtering criteria</returns>
        public override IEnumerable<Lang> Find(LangFilter filter)
        {
            Criteria.Append(Apply(filter));

            return GetListByCriteria(filter);
        }

        protected override IList<ICriterion> Apply(LangFilter filter)
        {
            var restrictions = base.Apply(filter);

            restrictions.AddCompare(filter, f => f.IdExact); // Searches for exact ID
            restrictions.AddIn(filter, f => f.IDColl);

            restrictions.AddEqual(filter, f => f.LanguageNameSearch);
            restrictions.AddLike(filter, f => f.LanguageName);

            return restrictions;
        }
        #endregion Find

        #region ILangDao Members
        /// <summary>
        ///     Returns the list of all currently available languages in application
        ///     Encapsulated in try-catch to allow bubble when DB is down.
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<Lang> GetAll()
        {
            var langs = Session.CreateCriteria(typeof(Lang)).List<Lang>();
            foreach (var lang in langs)
            {
                Session.Evict(lang);
            }
            return langs;
        }
        #endregion
    }
}